export const fileHandle = (fileData, handleKind, headers, downloadFileName) => {
    let blobData;
    switch (Object.prototype.toString.call(fileData)) {
        case "[object Blob]":
            blobData = fileData;
            break;
        case "[object ArrayBuffer]":
            blobData = new Blob([fileData]);
            break;
    }

    let BlobUrl = URL.createObjectURL(blobData);
    switch (handleKind) {
        case "preview":
            return BlobUrl;

        case "download":
            {
                let { "content-disposition": contentDisposition } = headers;

                let serveFileName;
                try {
                    serveFileName = /filename="?'?([^;"']+)/g.exec(
                        contentDisposition
                    )[1];
                } catch {
                    serveFileName = "";
                }

                let aNode = document.createElement("a");
                aNode.href = BlobUrl;
                aNode.download =
                    downloadFileName || decodeURIComponent(serveFileName);
                document.body.appendChild(aNode);
                aNode.click();
                document.body.removeChild(aNode);
                URL.revokeObjectURL(BlobUrl);
            }

            break;
    }
};
